package rules.tree;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.jdom.Element;

public class Atom {
	protected Element el;
        protected ArrayList<Object> args;

	public Atom(Element el) {
		this.el = el;
	}

        public  Element getElement(){
            return el;
        }
        	
	public String getTypeAtom(){
		if (el.getName().equals("classAtom"))
			return "classAtom";
		else if (el.getName().equals("propertyAtom"))
			return "propertyAtom";
		else if (el.getName().equals("builtinAtom"))
			return "builtinAtom";
		else if (el.getName().equals("restrictionBody"))
			return "restrictionBody";
		else if (el.getName().equals("restrictionHead"))
			return "restrictionHead";
		else return "context"; 
	}

        public void addArg(Var v){
            el.addContent(new Element("args"));
            el.getChild("args").addContent(v.getElement());
        }

//	public String getTypeAtom() {
//		if (el.getChild("classAtom") != null)
//			return "classAtom";
//		else if (el.getChild("propertyAtom") != null)
//			return "propertyAtom";
//		else if (el.getChild("builtinAtom") != null)
//			return "builtinAtom";
//		else if (el.getChild("restrictionBody") != null)
//			return "restrictionBody";
//		else if (el.getChild("restrictionHead") != null)
//			return "restrictionHead";
//		else return "context";
//	}

	public ClassAtom getClassAtom() {
		ClassAtom ca = null;
		//Element classAtom = el.getChild("classAtom");
		Element classAtom = el;
		String attribute = classAtom.getAttribute("class").getValue();
		Element args = classAtom.getChild("args");
		Element arg = (Element) args.getChildren().get(0);
		String name = arg.getName();
		if(name.equals("var")){
			Var v1 = new Var(arg);
			ca = new ClassAtom(attribute, v1);
		}else if (name.equals("constant")){
			ca = new ClassAtom(attribute, arg.getValue());
		}else{
			Function f1 = new Function(arg);
			ca = new ClassAtom(attribute, f1);
		}
		return ca;
	}

	public PropertyAtom getPropertyAtom() {
		PropertyAtom pa = null;
		Var v1, v2;
		Function f1, f2;
		//Element propertyAtom = el.getChild("propertyAtom");
		Element propertyAtom = el;
		String attribute = propertyAtom.getAttribute("property").getValue();
		Element args = propertyAtom.getChild("args");
		//List temp = propertyAtom.getChildren();
		List temp = args.getChildren();
		Iterator it = temp.iterator();
		Element arg1 = ((Element) it.next());
		String name1 = arg1.getName(); 
		Element arg2 = ((Element) it.next());
		String name2 = arg2.getName();
		if(name1.equals("var") && name2.equals("var")){
			v1 = new Var(arg1);
			v2 = new Var(arg2);
			pa = new PropertyAtom(attribute, v1, v2);
		}else if (name1.equals("var") && name2.equals("constant")){
			v1 = new Var(arg1);
			pa = new PropertyAtom(attribute, v1, arg2.getValue());
		}else if (name1.equals("constant") && name2.equals("var")){
			v2 = new Var(arg2);
			pa = new PropertyAtom(attribute, v2, arg1.getValue());
		}else if (name1.equals("var") && name2.equals("function")){
			v1 = new Var(arg1);
			f2 = new Function(arg2);
			pa = new PropertyAtom(attribute, v1, f2);
		}else if (name1.equals("function") && name2.equals("var")){
			v2 = new Var(arg2);
			f1 = new Function(arg1);
			pa = new PropertyAtom(attribute, v2, f1);
		}else if (name1.equals("function") && name2.equals("constant")){
			f1 = new Function(arg1);
			pa = new PropertyAtom(attribute, f1, arg2.getValue());
		}else if (name1.equals("constant") && name2.equals("function")){
			f2 = new Function(arg2);
			pa = new PropertyAtom(attribute, f2, arg1.getValue());
		}else{ //function and function
			f1 = new Function(arg1);
			f2 = new Function(arg2);
			pa = new PropertyAtom(attribute, f1, f2);
		}
		return pa;
	}

	public BuiltinAtom getBuiltinAtom() {
		BuiltinAtom ba = null;
		Var v1, v2;
		Function f1, f2;
		//Element builtinAtom = el.getChild("builtinAtom");
		Element builtinAtom = el;
		String attribute = builtinAtom.getAttribute("builtin").getValue();
		Element args = builtinAtom.getChild("args");
		//List temp = builtinAtom.getChildren();
		List temp = args.getChildren();
		Iterator it = temp.iterator();
		Element arg1 = ((Element) it.next());
		String name1 = arg1.getName(); 
		Element arg2 = ((Element) it.next());
		String name2 = arg2.getName();
		if(name1.equals("var") && name2.equals("var")){
			v1 = new Var(arg1);
			v2 = new Var(arg2);
			ba = new BuiltinAtom(attribute, v1, v2);
		}else if (name1.equals("var") && name2.equals("constant")){
			v1 = new Var(arg1);
			ba = new BuiltinAtom(attribute, v1, arg2.getValue());
		}else if (name1.equals("constant") && name2.equals("var")){
			v2 = new Var(arg2);
			ba = new BuiltinAtom(attribute, v2, arg1.getValue());
		}else if (name1.equals("var") && name2.equals("function")){
			v1 = new Var(arg1);
			f2 = new Function(arg2);
			ba = new BuiltinAtom(attribute, v1, f2);
		}else if (name1.equals("function") && name2.equals("var")){
			v2 = new Var(arg2);
			f1 = new Function(arg1);
			ba = new BuiltinAtom(attribute, v2, f1);
		}else if (name1.equals("function") && name2.equals("constant")){
			f1 = new Function(arg1);
			ba = new BuiltinAtom(attribute, f1, arg2.getValue());
		}else if (name1.equals("constant") && name2.equals("function")){
			f2 = new Function(arg2);
			ba = new BuiltinAtom(attribute, f2, arg1.getValue());
		}else{ //function and function
			f1 = new Function(arg1);
			f2 = new Function(arg2);
			ba = new BuiltinAtom(attribute, f1, f2);
		}
		return ba;
	}

        

//	public Restriction getRestrictionBody() {
//		//Element restriction = el.getChild("restrictionBody");
//		Element restriction = el;
//		List<Atom> list = restriction.getChildren();
//		Restriction rest = new Restriction(list);
//		return rest;
//	}
//
//	public Restriction getRestrictionHead() {
//		//Element restriction = el.getChild("restrictionHead");
//		Element restriction = el;
//		List<Atom> list = restriction.getChildren();
//		Restriction rest = new Restriction(list);
//		return rest;
//	}
//
//	public Context getContext() {
//		//Element context = el.getChild("context");
//		Element context = el;
//		List<Atom> list = context.getChildren();
//		Context cont = new Context(list);
//		return cont;
//	}
}
